*create correct contiguity file (commented out)

/*
clear
cd "G:/Other computers/Mijn Laptop/Research/Protests/Contiguity matrix"


spshape2dta tl_2020_us_county
clear
use tl_2020_us_county
quietly destring STATEFP, generate(st_statefp)
*keep continental US counties
drop if st_statefp==2 | st_statefp==15 | st_statefp>56
rename _CX longitude
rename _CY latitude
generate GEOID2 = GEOID
save county, replace
*/


clear

*set your work directory
cd "" 
set maxvar 7000

******** Create contiguity matrices *************


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
spmat idistance dcounty_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp using "SpatialMats/spmat_unemployment", replace



clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_1)
drop if missing(av_rainprob_period_1)
spmat idistance dcounty_period1_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_2)
drop if missing(av_rainprob_period_2)
spmat idistance dcounty_period2_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_3)
drop if missing(av_rainprob_period_3)
spmat idistance dcounty_period3_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_4)
drop if missing(av_rainprob_period_4)
spmat idistance dcounty_period4_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_5)
drop if missing(av_rainprob_period_5)
spmat idistance dcounty_period5_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_6)
drop if missing(av_rainprob_period_6)
spmat idistance dcounty_period6_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace



clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_7)
drop if missing(av_rainprob_period_7)
spmat idistance dcounty_period7_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_8)
drop if missing(av_rainprob_period_8)
spmat idistance dcounty_period8_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(prcp_total_period_9)
drop if missing(av_rainprob_period_9)
spmat idistance dcounty_period9_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(no_favors_blacks)
drop if missing(slavery_to_discrim)
spmat idistance dcounty_race1_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace



clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if missing(no_favors_blacks)
drop if missing(slavery_to_discrim)
spmat idistance dcounty_race2_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if rep_4_8_12==1
spmat idistance dcounty_repsonly_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if dem_4_8_12==1
spmat idistance dcounty_demsonly_unemp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace


*get alternative Ws
clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
spmat idistance dcounty_unemp_euc longitude latitude, id(fips_code_5char) dfunction(euclidean) normalize(minmax) replace
spmat save dcounty_unemp_euc using "SpatialMats/spmat_unemployment_euc", replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
spmat idistance dcounty_unemp_rhav longitude latitude, id(fips_code_5char) dfunction(rhaversine) normalize(minmax) replace
spmat save dcounty_unemp_rhav using "SpatialMats/spmat_unemployment_rhav", replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
spmat idistance dcounty_unemp_mink1 longitude latitude, id(fips_code_5char) dfunction(1) normalize(minmax) replace
spmat save dcounty_unemp_mink1 using "SpatialMats/spmat_unemployment_mink1", replace

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
spmat idistance dcounty_unemp_mink2 longitude latitude, id(fips_code_5char) dfunction(2) normalize(minmax) replace
spmat save dcounty_unemp_mink2 using "SpatialMats/spmat_unemployment_mink2", replace

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
spmat idistance dcounty_unemp_mink3 longitude latitude, id(fips_code_5char) dfunction(3) normalize(minmax) replace
spmat save dcounty_unemp_mink3 using "SpatialMats/spmat_unemployment_mink3", replace




clear
import delim "fData_cces_clean_oneobs.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
spmat idistance dcounty_unemp_cces longitude latitude, id(fips) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_cces using "SpatialMats/spmat_unemployment_cces.dta", replace





*heterogeneity analyses
clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if black_2019>median_black
spmat idistance dcounty_unemp_abovemedianblack longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_abovemedianblack using "SpatialMats/spmat_unemployment_abovemedianblack.dta", replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if black_2019<=median_black
spmat idistance dcounty_unemp_belowmedianblack longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_belowmedianblack using "SpatialMats/spmat_unemployment_belowmedianblack.dta", replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if total_population_2019>median_population
spmat idistance dcounty_unemp_abovemedianpop longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_abovemedianpop using "SpatialMats/spmat_unemployment_abovemedianpop.dta", replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if total_population_2019<=median_population
spmat idistance dcounty_unemp_belowmedianpop longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_belowmedianpop using "SpatialMats/spmat_unemployment_belowmedianpop.dta", replace



clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if graduate_frac>median_graduate
spmat idistance dcounty_unemp_abovemediangrad longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_abovemediangrad using "SpatialMats/spmat_unemployment_abovemediangrad.dta", replace


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
keep if graduate_frac<=median_graduate
spmat idistance dcounty_unemp_belowmediangrad longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_belowmediangrad using "SpatialMats/spmat_unemployment_belowmediangrad.dta", replace



*other weather measures
clear
import delim "Final_data_counties_stata_new_unemployment.csv"
gen GEOID2 = subinstr(geoid, "G", "",.) 
merge 1:1 GEOID2 using county 
keep if station_normal!=""
drop if tmax_max == .
drop if tmin_min == .
drop if tmax_average == .
spmat idistance dcounty_unemp_temp longitude latitude, id(fips_code_5char) dfunction(dhaversine) normalize(minmax) replace
spmat save dcounty_unemp_temp using "SpatialMats/spmat_unemployment_temp.dta", replace



*************** Table 2: Main estimates, Table A3 Panel A: days of protests *******************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"

eststo: spivreg change_frac_dem (days_of_protests=prcp_total) av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019  , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (days_of_protests=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (days_of_protests=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)


eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019  , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)


esttab  using Tables/Spatial_IV_main.tex, replace

mat Coefs_main = r(coefs)
putexcel set "Tables/Results_main.xlsx",replace
putexcel A1=matrix(Coefs_main), names



************* Table 4, Panel A: Turnout analysis ******************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"	


eststo: spivreg change_turnout_rate (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019  , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_turnout_rate (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)


eststo: spivreg change_turnout_rate (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)


esttab  using Tables/Spatial_IV_turnout.tex, replace

mat Coefs_turnout = r(coefs)
putexcel set "Tables/Results_turnout.xlsx",replace
putexcel A1=matrix(Coefs_turnout), names


************ Table A6: First stage ****************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"


eststo: spreg gs2sls  estimate_low_tot_pop_100 prcp_total av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019  , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spreg gs2sls  estimate_low_tot_pop_100 prcp_total  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)


eststo: spreg gs2sls  estimate_low_tot_pop_100 prcp_total  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)




esttab  using Tables/Spatial_IV_main_firststage.tex, replace

mat Coefs_main_firststage = r(coefs)
putexcel set "Tables/Results_main_firststage.xlsx",replace
putexcel A1=matrix(Coefs_main_firststage), names



************* Table A7, Panel A: Reduced form 2020 *****************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
eststo: spreg gs2sls change_frac_dem prcp_total av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019  , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)
eststo: spreg gs2sls change_frac_dem prcp_total av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)
eststo: spreg gs2sls change_frac_dem prcp_total av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

esttab  using Tables/Spatial_IV_reduced_2020.tex, replace

mat Coefs_reduced = r(coefs)
putexcel set "Tables/Results_reduced.xlsx",replace
putexcel A1=matrix(Coefs_reduced), names

*************  Table A7, Panel B: Reduced form 2016 *****************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
eststo: spreg gs2sls change_frac_dem_2016 prcp_total  av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019 , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)
eststo: spreg gs2sls change_frac_dem_2016 prcp_total  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)
eststo: spreg gs2sls change_frac_dem_2016 prcp_total  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

esttab  using Tables/Spatial_IV_reduced_2016.tex, replace

mat Coefs_reduced_2016 = r(coefs)
putexcel set "Tables/Results_reduced_2016.xlsx",replace
putexcel A1=matrix(Coefs_reduced_2016), names


*************  Table A7, Panel C: Reduced form 2012 *****************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
eststo: spreg gs2sls change_frac_dem_2012 prcp_total  av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019 , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)
eststo: spreg gs2sls change_frac_dem_2012 prcp_total  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)
eststo: spreg gs2sls change_frac_dem_2012 prcp_total  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

esttab  using Tables/Spatial_IV_reduced_2012.tex, replace

mat Coefs_reduced_2012 = r(coefs)
putexcel set "Tables/Results_reduced_2012.xlsx",replace
putexcel A1=matrix(Coefs_reduced_2012), names

*********** Table A8: Exclusion period 1-9 *****************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_1)
drop if missing(av_rainprob_period_1)
eststo: spreg gs2sls change_frac_dem prcp_total_period_1 av_rainprob_period_1 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period1_unemp) elmat(dcounty_period1_unemp)

mat Coefs_exclusion_1 = r(table)
putexcel set "Tables/Results_exclusion_1.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_1), names

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_2)
drop if missing(av_rainprob_period_2)
eststo: spreg gs2sls change_frac_dem prcp_total_period_2 av_rainprob_period_2 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period2_unemp) elmat(dcounty_period2_unemp)

mat Coefs_exclusion_2 = r(table)
putexcel set "Tables/Results_exclusion_2.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_2), names

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_3)
drop if missing(av_rainprob_period_3)
eststo: spreg gs2sls change_frac_dem prcp_total_period_3 av_rainprob_period_3 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period3_unemp) elmat(dcounty_period3_unemp)

mat Coefs_exclusion_3 = r(table)
putexcel set "Tables/Results_exclusion_3.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_3), names

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_4)
drop if missing(av_rainprob_period_4)
eststo: spreg gs2sls change_frac_dem prcp_total_period_4 av_rainprob_period_4 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period4_unemp) elmat(dcounty_period4_unemp)

mat Coefs_exclusion_4 = r(table)
putexcel set "Tables/Results_exclusion_4.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_4), names

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_5)
drop if missing(av_rainprob_period_5)
eststo: spreg gs2sls change_frac_dem prcp_total_period_5 av_rainprob_period_5 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period5_unemp) elmat(dcounty_period5_unemp)

mat Coefs_exclusion_5 = r(table)
putexcel set "Tables/Results_exclusion_5.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_5), names

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_6)
drop if missing(av_rainprob_period_6)
eststo: spreg gs2sls change_frac_dem prcp_total_period_6 av_rainprob_period_6 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period6_unemp) elmat(dcounty_period6_unemp)

mat Coefs_exclusion_6 = r(table)
putexcel set "Tables/Results_exclusion_6.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_6), names


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_7)
drop if missing(av_rainprob_period_7)
eststo: spreg gs2sls change_frac_dem prcp_total_period_7 av_rainprob_period_7 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period7_unemp) elmat(dcounty_period7_unemp)

mat Coefs_exclusion_7 = r(table)
putexcel set "Tables/Results_exclusion_7.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_7), names

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_8)
drop if missing(av_rainprob_period_8)
eststo: spreg gs2sls change_frac_dem prcp_total_period_8 av_rainprob_period_8 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period8_unemp) elmat(dcounty_period8_unemp)

mat Coefs_exclusion_8 = r(table)
putexcel set "Tables/Results_exclusion_8.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_8), names



clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(prcp_total_period_9)
drop if missing(av_rainprob_period_9)
eststo: spreg gs2sls change_frac_dem prcp_total_period_9 av_rainprob_period_9 totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_period9_unemp) elmat(dcounty_period9_unemp)

mat Coefs_exclusion_9 = r(table)
putexcel set "Tables/Results_exclusion_9.xlsx",replace
putexcel A1=matrix(Coefs_exclusion_9), names

************* Table 4, Panel B: Race attitudes 1 **************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(no_favors_blacks)
drop if missing(slavery_to_discrim)
eststo: spivreg no_favors_blacks (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  , id(fips_code_5char) dlmat(dcounty_race1_unemp) elmat(dcounty_race1_unemp)
eststo: spivreg no_favors_blacks (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_race1_unemp) elmat(dcounty_race1_unemp)
eststo: spivreg no_favors_blacks (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_race1_unemp) elmat(dcounty_race1_unemp)


esttab  using Tables/Spatial_IV_race1.tex, replace

mat Coefs_race_1 = r(coefs)
putexcel set "Tables/Results_race_1.xlsx",replace
putexcel A1=matrix(Coefs_race_1), names


************* Table 4, Panel C: Race attitudes 2 **************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if missing(no_favors_blacks)
drop if missing(slavery_to_discrim)
eststo: spivreg slavery_to_discrim (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  , id(fips_code_5char) dlmat(dcounty_race2_unemp) elmat(dcounty_race2_unemp)
eststo: spivreg slavery_to_discrim (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_race2_unemp) elmat(dcounty_race2_unemp)
eststo: spivreg slavery_to_discrim (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_race2_unemp) elmat(dcounty_race2_unemp)


esttab  using Tables/Spatial_IV_race2.tex, replace

mat Coefs_race_2 = r(coefs)
putexcel set "Tables/Results_race_2.xlsx",replace
putexcel A1=matrix(Coefs_race_2), names

*************** Table A3, Panel B: Two instruments ***************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total days_of_rain) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total days_of_rain) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total days_of_rain) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)


esttab  using Tables/Spatial_IV_twoinst.tex, replace

mat Coefs_twoinst = r(coefs)
putexcel set "Tables/Results_twoinst.xlsx",replace
putexcel A1=matrix(Coefs_twoinst), names






************ Table A2: Robustness to different Ws *******************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_euc) elmat(dcounty_unemp_euc)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_rhav) elmat(dcounty_unemp_rhav)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_mink1) elmat(dcounty_unemp_mink1)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_mink2) elmat(dcounty_unemp_mink2)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_mink3) elmat(dcounty_unemp_mink3)



esttab  using Tables/Spatial_IV_altW.tex, replace

mat Coefs_main_altw = r(coefs)
putexcel set "Tables/Results_main_altW.xlsx",replace
putexcel A1=matrix(Coefs_main_altw), names





***************** Table A4: state-level dummies *********************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
encode state, generate(state_cat)



eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019  i.state_cat  , id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019  i.state_cat median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019  i.state_cat median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp) elmat(dcounty_unemp)


esttab  using Tables/Spatial_IV_statecat.tex, replace

mat Coefs_main = r(coefs)
putexcel set "Tables/Results_statecat.xlsx",replace
putexcel A1=matrix(Coefs_main), names





************ Table 3, Panel A: Heterogeneity: Race ************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
keep if black_2019<=median_black



eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_belowmedianblack) elmat(dcounty_unemp_belowmedianblack)

clear
import delim "Final_data_counties_stata_new_unemployment.csv"

keep if black_2019>median_black
eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_abovemedianblack) elmat(dcounty_unemp_abovemedianblack)


esttab  using Tables/Spatial_IV_heteroblack.tex, replace



mat Coefs_main_heteroblack = r(coefs)
putexcel set "Tables/Results_main_heteroblack.xlsx",replace
putexcel A1=matrix(Coefs_main_heteroblack), names




************  Table 3, Panel B: Heterogeneity: Pop size ************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
keep if total_population_2019<=median_population



eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_belowmedianpop) elmat(dcounty_unemp_belowmedianpop)

clear
import delim "Final_data_counties_stata_new_unemployment.csv"
keep if total_population_2019>median_population


eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_abovemedianpop) elmat(dcounty_unemp_abovemedianpop)

esttab  using Tables/Spatial_IV_heteropop.tex, replace



mat Coefs_main_heteropop = r(coefs)
putexcel set "Tables/Results_main_heteropop.xlsx",replace
putexcel A1=matrix(Coefs_main_heteropop), names




************  Table 3, Panel C: Heterogeneity: education ************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
keep if graduate_frac<=median_graduate
eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_belowmediangrad) elmat(dcounty_unemp_belowmediangrad)


clear
import delim "Final_data_counties_stata_new_unemployment.csv"
keep if graduate_frac>median_graduate

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_abovemediangrad) elmat(dcounty_unemp_abovemediangrad)



esttab  using Tables/Spatial_IV_heterograd.tex, replace



mat Coefs_main_heterograd = r(coefs)
putexcel set "Tables/Results_main_heterograd.xlsx",replace
putexcel A1=matrix(Coefs_main_heterograd), names




************* Table A9: First stage with other weather patterns **************

clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if tmax_max == .
drop if tmin_min == .
drop if tmax_average == .

eststo: spreg gs2sls  estimate_low_tot_pop_100 prcp_total tmax_max tmax_average av_rainprob totalpop2019_100000 cases deaths density white_2019 black_2019 hispanic_2019 asian_2019  , id(fips_code_5char) dlmat(dcounty_unemp_temp) elmat(dcounty_unemp_temp)

eststo: spreg gs2sls  estimate_low_tot_pop_100 prcp_total tmax_max tmax_average  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp_temp) elmat(dcounty_unemp_temp)


eststo: spreg gs2sls  estimate_low_tot_pop_100 prcp_total tmax_max tmax_average  av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_temp) elmat(dcounty_unemp_temp)


esttab  using Tables/Spatial_IV_main_firststage_temp.tex, replace

mat Coefs_main_firststage_temp = r(coefs)
putexcel set "Tables/Results_main_firststage_temp.xlsx",replace
putexcel A1=matrix(Coefs_main_firststage_temp), names




************** Table A3, Panel C, Main analysis with temperature *******************
clear
eststo clear
import delim "Final_data_counties_stata_new_unemployment.csv"
drop if tmax_max == .
drop if tmin_min == .
drop if tmax_average == .

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total tmax_max tmax_average) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 , id(fips_code_5char) dlmat(dcounty_unemp_temp) elmat(dcounty_unemp_temp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total tmax_max tmax_average) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac, id(fips_code_5char) dlmat(dcounty_unemp_temp) elmat(dcounty_unemp_temp)

eststo: spivreg change_frac_dem (estimate_low_tot_pop_100=prcp_total tmax_max tmax_average) av_rainprob totalpop2019_100000 cases deaths density  white_2019 black_2019 hispanic_2019 asian_2019 median_age_2019 less_than_highschool_frac highschool_frac some_college_frac bachelor_frac graduate_frac median_income_2019 unemployment_2019, id(fips_code_5char) dlmat(dcounty_unemp_temp) elmat(dcounty_unemp_temp)


esttab  using Tables/Spatial_IV_temp.tex, replace

mat Coefs_temp = r(coefs)
putexcel set "Tables/Results_temp.xlsx",replace
putexcel A1=matrix(Coefs_temp), names



